<?php

namespace app\admin\controller;

use app\api\model\Privatechat;
use app\api\model\YaoqingRecords;
use think\Db;
use think\Validate;

class Matchmaking extends Common {

    public function pullLine()
    {
        $map = [];
        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $map[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('hunjie_vip_style')->where($map)->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addPullLine()
    {

        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('hunjie_vip_style')->insert($post);

                $data = $db ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $db = Db::name('hunjie_vip_style')->where('id', $id)->update($post);

                $data = $db ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('hunjie_vip_style')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function delPullLine()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('hunjie_vip_style')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function partnerList()
    {
        $map = [];
        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $map[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('hunjie_partner')->where($map)->order('sort desc, id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addPartner()
    {

        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('hunjie_partner')->insert($post);

                $data = $db ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $db = Db::name('hunjie_partner')->where('id', $id)->update($post);

                $data = $db ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('hunjie_partner')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function hehuo()
    {
        $map = [];
        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $map[$search_type] = ['like', '%' . $search_name . '%'];
        } else {
            $map = '';
        }

        $config = Db::name('config_system')->column('title, content');

        $config['daili_company'] = (1 - $config['matchmaking_shouyi_1'] - $config['matchmaking_shouyi_2'] - $config['matchmaking_shouyi_3']);
        $config['pull_company'] = (1 - $config['pullline_shouyi_1'] - $config['pullline_shouyi_2'] - $config['pullline_shouyi_3']);
        $config['vip_company'] = (100 - $config['vip_shouyi_1'] - $config['vip_shouyi_2'] - $config['vip_shouyi_3']);

        $partner = Db::name('hunjie_partner')->field('id, price, name')->order('id desc')->select();
        $pull = Db::name('hunjie_vip_style')->field('id, price, description')->order('id asc')->select();
        $vip = Db::name('vip_style')->field('id, description, price')->order('id asc')->select();

        $this->assign('partner', $partner);
        $this->assign('pull', $pull);
        $this->assign('vip', $vip);
        $this->assign('config', $config);

        return $this->fetch();
    }


    public function partner()
    {
        $where = [];

        $types = input('types');
        if ($types) {
            $where['t1.types'] = $types;
        }

        $status = input('status');
        if ($status) {
            $where['t1.status'] = $status;
        }

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where['t2.' . $search_type] = $search_name;
        }

        $data = Db::name('hunjie_partner_apply t1')
            ->field('t1.*, t2.name,t2.phone')
            ->join('user t2', 't2.id = t1.user_id')
            ->where($where)->order('t1.id desc')
            ->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);
        $this->assign('status', $status);

        return $this->fetch();
    }

    public function partnerEdit()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if ($id) {
                $apply = Db::name('hunjie_partner_apply')->find($id);
                $order = Db::name('order_common')->where(['types'=>10, 'user_id'=>$apply['user_id'], 'status'=>1])->order('id desc')->find();

                if ($post['status'] == -1) {
                    // Balance::index('volley', $apply['user_id'], $order['money'] * getenv('APP_RATE'), '合伙人申请未通过', 1);
                } else if ($post['status'] == 1) {
                    Db::name('user_profile')->where('user_id', $apply['user_id'])->setField('partner', $apply['type']);
                }

                $result = Db::name('hunjie_partner_apply')->where('id', $id)->update(['status' => $post['status'], 'update_time' => time()]);

                $data = $result ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            } else {
                $data = ["err" => '参数缺失'];
            }
        }
        return $data;
    }

    public function partnerDel()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('hunjie_partner_apply')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function Store()
    {
        $map = [];
        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            $map[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('hunjie_store')->where($map)->order('id asc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function addStore()
    {

        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            if (!$id) {
                $db = Db::name('hunjie_store')->insert($post);

                $data = $db ? ['ret' => '添加成功'] : ['err' => '添加失败'];
            } else {
                $db = Db::name('hunjie_store')->where('id', $id)->update($post);

                $data = $db ? ['ret' => '修改成功'] : ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('hunjie_store')->find($id);
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }

    public function delStore()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('hunjie_store')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }

    public function UserProfile()
    {
        $map = [];
        $search_name = input('param.name');
        $search_type = input('param.type');
        if ($search_name && $search_type) {
            if ($search_type == 'user_id') {
                $map[$search_type] = $search_name;
            } else {
                $map[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $type = input('type/d', 0);
        $this->assign('type', $type);
        if ($type == 1) {
            $map['proxy'] = 1;
        } else if ($type == 2) {
            $map['partner'] = ['>', 0];
        }

        $data = Db::name('user_profile')->where($map)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }


    public function addUserProfile()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');

            $user_profile = Db::name('user_profile')->where('id', $id)->find();

            $user_profile_fields = Db::getTableInfo('user_profile', 'fields');
            $user_fields = Db::getTableInfo('user', 'fields');
            $user_fields_filter = ['head_portrait','name'];
            $hunjie_proxy_fields = Db::getTableInfo('hunjie_proxy', 'fields');
            $hunjie_proxy_in = ['proxy_name'=>'name', 'proxy_phone'=>'phone', 'proxy_city'=>'city', 'proxy_idcard'=>'idcard'];

            $update_user_profile = [];
            $update_user = [];
            $update_hunjie_proxy = [];
            foreach ($post as $key => $value) {
                if ($key == 'id') continue;
                if (in_array($key, $user_profile_fields)) {
                    $update_user_profile[$key] = $value;
                }
                if (in_array($key, $user_fields) && !in_array($key, $user_fields_filter)) {
                    $update_user[$key] = $value;
                }
                if (in_array($key, array_keys($hunjie_proxy_in))) {
                    $update_hunjie_proxy[$hunjie_proxy_in[$key]] = $value;
                }
            }
            $db = Db::name('user_profile')->where('id', $id)->update($update_user_profile);
            Db::name('user')->where('id', $user_profile['user_id'])->update($update_user);
            Db::name('hunjie_proxy')->where('user_id', $user_profile['user_id'])->update($update_hunjie_proxy);
            if ($db !== false) {
                $data = ['ret' => '修改成功'];
            } else {
                $data = ['err' => '修改失败'];
            }
        } else {
            if ($id) {
                $name = Db::name('user_profile')->where('id', $id)->find();
                if ($name) {
                    $user = Db::name('user')->find($name['user_id']);
                    $name = array_merge($user, $name);
                    $proxy_info = Db::name('hunjie_proxy')->where(['user_Id'=>$name['user_id']])->find();
                    $this->assign('proxy_info', $proxy_info);
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $occupation = Db::name('user_occupation')->where(['hide' => 0])->order('des desc, id asc')->select();
            $this->assign('occupation', $occupation);
            $matchmaker = Db::name('user_profile')->where(['proxy' => 1])->order('id asc')->select();
            $this->assign('matchmaker', $matchmaker);

            $data = $this->fetch();
        }
        return $data;
    }


}